const BDUtil = function(array = []) {
  const h = window._hmt ? window._hmt : null
  h && h.push && h.push(array)
  if (!h) {
    console.error('BDUtil 没有得到值 h=', h)
  }
}

export default class BaiduVitals {
  static load(name, obj) {
    this.trackEvent({
      category: name,
      action: '加载',
      opt_label: obj
    })
  }

  static click(name, obj) {
    this.trackEvent({
      category: name,
      action: '点击',
      opt_label: obj
    })
  }

  static custom(phone, obj) {
    const index = phone
    if (!index) {
      if (isDev()) {
        console.log('\n custom 用户行为-百度统计 => index=null')
      }
      return
    }
    let name = ''
    let value = ''
    if (!obj) {
      if (isDev()) {
        console.log('\n custom 用户行为-百度统计 => obj=null')
      }
      return
    }
    for (const key in obj) {
      if (key === 'name') {
        name = obj[key]
      } else {
        if (value) {
          value += '|'
        }
        value += key
        value += ':'
        value += obj[key]
      }
    }
    // console.log('value=', value, typeof value)
    this.tranckCustomVar({
      index, name, value
    })
  }

  /**
   * 百度 - 事件统计
   * http://tongji.baidu.com/open/api/more?p=ref_trackEvent
   * @param {
   * cate: string 必传,
   * action: string 必传,
   * opt_label?: array|string 可选,
   * opt_value?: number 可选
   * } event 参数
   */
  static trackEvent(event = {
    category: '',
    action: '',
    opt_label: null,
    opt_value: 0,
  }) {
    const safeEvent = event || {}
    const eventId = '_trackEvent'
    const category = safeEvent.category || '-'
    const action = safeEvent.action || '-'
    const opt_labelArray = safeEvent.opt_label || '-'
    const opt_value = safeEvent.opt_value || 0

    let opt_label = '-'
    if (opt_labelArray && Array.isArray(opt_labelArray) && opt_labelArray.length) {
      const ol_value = opt_labelArray.join('|') || ''
      if (ol_value && typeof ol_value === 'string') {
        opt_label = ol_value
      }
    } else if (opt_labelArray && typeof opt_labelArray === 'string') {
      opt_label = opt_labelArray
    }

    const array = [
      eventId,
      category,
      action,
      opt_label
    ]

    if (opt_value && typeof opt_value === 'number') {
      array.push(opt_value)
    }

    if (isDev()) {
      console.log('\n百度事件统计 =>', array)
    } else {
      BDUtil(array)
    }
  }

  /**
   * 百度 - 页面统计
   * http://tongji.baidu.com/open/api/more?p=ref_trackPageview
   * @param {string} url 当前页面url
   */
  static trackPageview(url) {
    url = url || ''
    if (isDev()) {
      console.log('\n百度页面page统计 =>', url)
    } else {
      BDUtil(['_trackPageview', url])
    }
  }

  /**
   * 百度 - 追踪用户使用行为
   * http://tongji.baidu.com/open/api/more?p=ref_setCustomVar
   * @param {
   * index: number 必传,
   * name: string 必传,
   * value: array|string 必传,
   * opt_scope: number 必传
   * } event 参数
   */
  static tranckCustomVar(event = {
    index: 0, // 索引的范围是从1到5
    name: '', // 每个索引对应的名字在使用一次后就会固定以后无法更改
    value: '',
    opt_scope: 3,
  }) {
    const safeEvent = event || {}
    const eventId = '_setCustomVar'
    const index = safeEvent.index || 0
    const name = safeEvent.name || ''
    const opt_value = safeEvent.value || ''
    const opt_scope = safeEvent.opt_scope || 3

    let value = ''
    if (opt_value && Array.isArray(opt_value) && opt_value.length) {
      const ol_value = opt_value.join('|') || ''
      if (ol_value && typeof ol_value === 'string') {
        value = ol_value
      }
    } else if (opt_value && typeof opt_value === 'string') {
      value = opt_value
    }

    const array = [
      eventId,
      index,
      name,
      value,
      opt_scope
    ]

    if (isDev()) {
      console.log('\n用户行为-百度统计 =>', array)
    } else {
      BDUtil(array)
    }
  }
}

function isDev() {
  try {
    if (process.env.NODE_ENV !== 'production') {
      return true
    }
  } catch (error) {
    console.error(error)
  }
  return false
}